package com.com.Demo;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ClientSocketDemo {

    public static void main(String[] args) throws IOException {
        //创建与 ServerSocket连接的客户端Socket对象
        Scanner scanner = new Scanner(System.in);
        while (true) {
            Socket clientSocket = new Socket("192.168.137.1", 8088);
            //客户端发送数据给服务端  通过socket得到输出流
            System.out.println("请输出要发送给服务端的数据,输入exit表示不再往服务端发送数据了");
            String msg = scanner.next();
//            DataOutputStream dos = new
//                    DataOutputStream(clientSocket.getOutputStream());
//            dos.writeUTF(msg);

                OutputStream outputStream = clientSocket.getOutputStream();
                outputStream.write(msg.getBytes());
            if ("exit".equals(msg)) {
                break;
            }


            InputStreamReader inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
            char[] buffer = new char[1024];
            int len = 0;
            if ((len = inputStreamReader.read(buffer)) > 0) {
                String str = new String(buffer, 0, len);
                System.out.println("客户端接收数据：" + str);
            }
//                String data=inputStreamReader.read();
//                DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
//                String data = dis.readUTF();
//                System.out.println("客户端接收服务器的响应：" + data);
//                dis.close();
//                dos.close();


        }

    }
}
